<template>
  <div class="custom-add">
    <header class="header-bar">
      <h2 class="title">添加应用</h2>
    </header>
    <el-form ref="form" :model="form" label-width="0">
      <div class="add-form">
        <div class="form-left">
          <el-form-item>
            <el-input v-model="form.url" placeholder="请输入网站网址"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="form.name" placeholder="输入网站名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="form.type_id" placeholder="请选择网站分类">
              <el-option v-for="(item, index) in appTypes" :label="item.text" :value="item.id" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="form.category" placeholder="请选择网站类型">
              <el-option label="工具" value="工具"></el-option>
              <el-option label="学习" value="学习"></el-option>
              <el-option label="交友" value="交友"></el-option>
              <el-option label="新闻" value="新闻"></el-option>
              <el-option label="资料" value="资料"></el-option>
              <el-option label="体育" value="体育"></el-option>
              <el-option label="摸鱼" value="摸鱼"></el-option>
              <el-option label="社区" value="社区"></el-option>
              <el-option label="视频" value="视频"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-color-picker v-model="form.background"></el-color-picker>
          </el-form-item>
        </div>
        <div class="icon-wrap">
          <el-form-item>
            <el-upload
              class="icon"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.icon" :src="form.icon" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <h2 class="app-title">{{form.name || '标题'}}</h2>
          </el-form-item>
        </div>
      </div>
      <el-form-item>
        <el-button type="primary" @click="createWebsite">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'custom-add',
  data () {
    return {
      appTypes: [],
      form: {
        url: '',
        icon: '',
        name: '',
        background: '',
        category: '',
        type_id: ''
      }
    }
  },
  watch: {
    'form.url' () {
      this.fetchWebsiteInfo()
    }
  },
  created () {
    this.fetchAppTypes()
  },
  methods: {
    async fetchWebsiteInfo () {
      const res = await this.$api.app.websiteInfo({
        url: this.form.url
      })
      console.log(res)
      this.form.name = res.data.title
      this.form.icon = res.data.icon
    },
    async fetchAppTypes () {
      const res = await this.$api.app.type()
      this.appTypes = res.data
    },
    async createWebsite () {
      try {
        await this.$api.app.create(this.form)
        alert('添加成功')
      } catch (e) {
        alert('添加失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.custom-add {
  padding: 20px 20px 0 20px;
  .header-bar {
    border-bottom: 1px solid #eee;
    margin-bottom: 20px;
    .title {
      font-size: 28px;
      line-height: 60px;
    }
  }
  .add-form {
    @include flex(row, flex-start, center);
    .form-left {
      flex: 1;
    }
    .icon-wrap {
      width: 200px;
      height: 100%;
      display: flex;
      justify-content: center;
      .icon {
        position: relative;
        width: 120px;
        height: 120px;
        text-align: center;
        line-height: 120px;
        overflow: hidden;
        border-radius: var(--icon-radius);
        font-size: 12px;
        transition: .2s;
        cursor: pointer;
        background: #eee;
        box-shadow: 0 0 5px #0000001a;
        /deep/.el-upload {
          width: 100%;
          height: 100%;
        }
        img {
          display: block;
          width: 100%;
          height: 100%;
          background-repeat: no-repeat;
          background-size: cover;
          background-color: #fff;
        }
      }
      .app-title {
        text-align: center;
        font-size: 12px;
      }
    }
  }
}
</style>
